package com.example.finalwork.lmpl;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.finalwork.DAO.IncomeDAO;
import com.example.finalwork.DB.Constants;
import com.example.finalwork.DB.DataBaseHelper;
import com.example.finalwork.Entity.Income;
import com.example.finalwork.LoginActivity;

import java.util.ArrayList;
import java.util.List;

public class IncomeDAOlmpl implements IncomeDAO {
    private final DataBaseHelper mDataBaseHelper;
    public IncomeDAOlmpl(Context context){
        mDataBaseHelper = new DataBaseHelper(context);
    }

    @Override
    public long addIncome(Income income) {
        SQLiteDatabase db = mDataBaseHelper.getWritableDatabase();
        ContentValues values = income2values(income);
        long result = db.insert(Constants.Income_Table_Name,null,values);
        db.close();
        return result;
    }
    @Override
    public int updateIncome(Income income){
        SQLiteDatabase db = mDataBaseHelper.getWritableDatabase();
        ContentValues values = income2values(income);
        int result = db.update(Constants.Income_Table_Name,values,"in_Id=?",new String[]{income.getIn_id()+""});
        db.close();
        return result;
    }

    @Override
    public int delIncomeById(int Id) {
        SQLiteDatabase db = mDataBaseHelper.getWritableDatabase();
        int result = db.delete(Constants.Income_Table_Name,"in_Id=? and in_User=?",new String[]{Id+"", LoginActivity.data});

        db.close();
        return result;
    }


    @Override
    public List<Income> GetAllIncome() {
        List<Income> incomes = new ArrayList<>();

        SQLiteDatabase db = mDataBaseHelper.getReadableDatabase();
        Cursor cursor = db.query(Constants.Income_Table_Name,null,null,null,null,null,null,null);
        while(cursor.moveToNext())
        {
            Income income = new Income();

            int in_Id = cursor.getInt(cursor.getColumnIndex("in_Id"));
            income.setIn_id(in_Id);

            String in_User = cursor.getString(cursor.getColumnIndex("in_User"));
            income.setIn_User(in_User);

            String in_Name = cursor.getString(cursor.getColumnIndex("in_Name"));
            income.setIn_Name(in_Name);

            String in_Date = cursor.getString(cursor.getColumnIndex("in_Date"));
            income.setIn_Date(in_Date);

            String in_Amount = cursor.getString(cursor.getColumnIndex("in_Amount"));
            income.setIn_Amount(in_Amount);

            String in_Remark = cursor.getString(cursor.getColumnIndex("in_Remark"));
            income.setIn_Remark(in_Remark);

            incomes.add(income);
        }
        db.close();
        return incomes;
    }

    @Override
    public List<Income> GetAllIncomeybydate() {
        List<Income> incomes = new ArrayList<>();

        SQLiteDatabase db = mDataBaseHelper.getReadableDatabase();

        String sql = "select in_User,in_Date,sum(in_Amount) as allmount from " + Constants.Income_Table_Name +  " group by " + Constants.columeindate;
        Cursor cursor = db.rawQuery(sql,null);
        while(cursor.moveToNext())
        {
            Income income = new Income();

            //int in_Id = cursor.getInt(cursor.getColumnIndex("in_Id"));
            //income.setIn_id(in_Id);

            String in_User = cursor.getString(cursor.getColumnIndex("in_User"));
            income.setIn_User(in_User);

            //String in_Name = cursor.getString(cursor.getColumnIndex("in_Name"));
            //income.setIn_Name(in_Name);

            String in_Date = cursor.getString(cursor.getColumnIndex("in_Date"));
            income.setIn_Date(in_Date);

            String in_Amount = cursor.getString(cursor.getColumnIndex("allmount"));
            income.setIn_Amount(in_Amount);

            //String in_Remark = cursor.getString(cursor.getColumnIndex("in_Remark"));
            //income.setIn_Remark(in_Remark);

            incomes.add(income);
        }
        db.close();
        return incomes;
    }


    private ContentValues income2values(Income income) {
        //添加数据
        ContentValues values = new ContentValues();
        values.put("in_Id",income.getIn_id());
        values.put("in_User",income.getIn_User());
        values.put("in_Name",income.getIn_Name());
        values.put("in_Date",income.getIn_Date());
        values.put("in_Amount",income.getIn_Amount());
        values.put("in_Remark",income.getIn_Remark());
        return values;
    }
}
